import os
import shutil
import sys

resPath = sys.argv[1]

# resPath = "/Users/pimingzhen/Works/Current/client-ftp-clear/android"

def findLatestApk(resDir):
    latestApkName = ""
    latestApkTime = 0
    for root, dirs, files in os.walk(resDir):
        for f in files:
            if f.find('.apk') != -1:
                fArr = f.split("_")
                dateS = fArr[-2]
                timeS = fArr[-1].split(".")[-2]
                # print(dateS, timeS)
                if latestApkTime < int(dateS + timeS):
                    latestApkTime = int(dateS + timeS)
                    latestApkName = f
    return latestApkName 

def clearOldVersionApk(resDir, latestApkName):
    for root, dirs, files in os.walk(resDir):
        for f in files:
            if f.find('.apk') != -1:
                if latestApkName == f:
                    print("skip  :" + os.path.join(resDir, f))
                else:
                    os.remove(os.path.join(resDir, f)) 
                    print("delete:" + os.path.join(resDir, f))

    return latestApkName

latestApkName = findLatestApk(resPath)

clearOldVersionApk(resPath, latestApkName)
